package data;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import domain.*;


public class JDBCRolDAO implements IRolDAO{
	
	private Connection conn;
	
	 public JDBCRolDAO() {
         conn = ConnectionManager.getInstance().checkOut();
        
     }
		
	  public List<Rol> selectAllRoles(){		  
		  PreparedStatement stmt = null;
		  
		  List<Rol> searchResults = new LinkedList();
		  ResultSet result = null;
		  
		  
		  try {

	            String sql = "SELECT * FROM Rol";
	            stmt = conn.prepareStatement(sql); //conn es una conexion valida
	            stmt.executeQuery();
	            result = stmt.executeQuery();

	            while (result.next()) {
	                Rol temp = new Rol();
	                temp.setId(result.getInt("id"));
	                temp.setNombre(result.getString("tipo"));
	                temp.setDs(result.getString("diaSemana"));
	                temp.setTurno(result.getString("turno"));
	                temp.setNumPlazasTotales(result.getInt("plazasTotales"));
	                searchResults.add(temp);
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            ConnectionManager.getInstance().checkIn(conn);
	            try {
	                if (result != null)
	                    result.close();
	                if (stmt != null)
	                    stmt.close();
	            } catch (SQLException e) {
	            }

	        }
	        return searchResults;
	    }
			  
		
		public int selectRol(Rol rol) {
		    PreparedStatement stmt = null;
		    ResultSet result = null;
		    int p = 0;
		   	
		    try {
		    	

		    	String sql = "SELECT * FROM rol WHERE (tipo = ?) AND (diaSemana = ?) AND (turno = ?)";

		        stmt = conn.prepareStatement(sql);
		        stmt.setString(1, rol.getNombre());
		        stmt.setString(2, rol.getDs());
		        stmt.setString(3, rol.getTurno());
		        result = stmt.executeQuery();
		
		        result.next();
		        System.out.println("OK");
		        p=result.getInt("id");
                		        
		    } catch (SQLException e) {
		        System.out.println("Message: " + e.getMessage());
		        System.out.println("SQLState: " + e.getSQLState());
		        System.out.println("ErrorCode: " + e.getErrorCode());
		    } finally {
		        try {
		            if (result != null) {
		                result.close();
		            }
		            if (stmt != null) {
		                stmt.close();
		            }
		        } catch (SQLException e) {
		        }
		    }
		    return p;
		}
		
		public String selectRolOID(String rolid) {
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	        String oidr = null;
	        String sql = "SELECT * FROM rol WHERE (id = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, rolid);
	            result = stmt.executeQuery();

	            result.next();
	            oidr = result.getString("id");
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return oidr;
	    }

}